<?php

namespace App\Admin\Controllers\Live;

use App\Admin\Repositories\CmcLiveStudio;
use App\Admin\Services\Live;
use App\Models\Goods\CmcGood;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB as CmcGoods;


class CmcLiveStudioController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */

    protected function grid()
    {
        return Grid::make(new CmcLiveStudio(), function (Grid $grid) {
            $grid->withBorder();
            $grid->quickSearch(['anchor_name', 'id']);
            $grid->column('id')->sortable();
            $grid->column('live_title','直播间标题');
            $grid->column('live_image','直播间封面')->image($server=null,100,100);
            $grid->column('start_play_time','开播时间');
            $grid->column('stop_play_time','停播时间');
            $grid->column('online_num','在线虚拟人数');
            $grid->column('anchor_name','主播名称');
            $grid->column('is_play', '是否正在开播')->switch()->filter(
                Grid\Column\Filter\In::make([
                    0=>'已结束',
                    1=>'直播中'
                ])
            );

            $grid->column('直播间')->modal(function ($modal) {
                //设置弹窗标题
                $modal->title('查看详情数据');
                $id = $this -> getKey();
                $liveStudio=\App\Models\Live\CmcLiveStudio::find($id);
                //弹窗内容
                return  view('admin/studio/studio',compact('liveStudio'));

            });


            $grid->column('查看')->modal(function ($modal) {
                //设置弹窗标题
                $modal->title('查看详情数据');
                $id = $this -> getKey();
                $liveStudio=\App\Models\Live\CmcLiveStudio::find($id);
                //弹窗内容
                return  view('admin/Live/analyze',compact('liveStudio'));

            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CmcLiveStudio(), function (Show $show) {
            $show->field('id','直播间ID');
            $show->field('stream_name','直播间号');
            $show->field('uid','用户id');
            $show->field('live_title','直播间标题');
            $show->field('live_image','直播间封面');
            $show->field('anchor_name','主播名称');
            $show->field('start_play_time','每天开播时间');
            $show->field('stop_play_time','每天停播时间');
            $show->field('live_desc','直播间简介');
            $show->field('online_num','虚拟在线人数');
            $show->field('studio_pwd','直播间密码');
            $show->field('auto_phrase','进入直播间自动回复');
            $show->field('add_time','添加时间');
            $show->field('is_del','是否删除');
            $show->field('is_play','是否正在开播');
            $show->field('is_playback','是否回放');
            $show->field('playback_record_id','回放RecordID');
            $show->field('sort','排序');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CmcLiveStudio(), function (Form $form) {
            $form->display('id');
            $form->text('stream_name','直播间号')->rules('required');
            $form->text('uid','用户id')->rules('required');
            $form->text('live_title','直播间标题')->rules('required');
            $form->image('live_image','直播间封面')->rules('required');
            $form->select('anchor_name','主播名称')->options(
                CmcGoods::table('cmc_live_anchor')->pluck('anchor_name','anchor_name')
            );
            $form->date('start_play_time','每天开播时间')->rules('required');
            $form->date('stop_play_time','每天停播时间')->rules('required');
            $form->editor('live_desc','直播间简介');
            $form->text('online_num','虚拟在线人数')->rules('required');
            $form->password('studio_pwd','直播间密码')->rules('required');
            $form->text('auto_phrase','进入直播间自动回复内容');
            $form->datetime('add_time','添加时间')->rules('required');
            $form->radio('is_del','是否删除')->options([0=>'未删除',1=>'删除'])->default(0)->rules('required');
            $form->radio('is_play','是否正在开播')->options([0=>'未开播',1=>'正在播',2=>'已结束'])->default(0)->rules('required');
            $form->radio('is_playback','是否回放')->options([0=>'不回放',1=>'回放'])->default(0)->rules('required');
            $form->text('playback_record_id','回放RecordID');
            $form->text('sort','排序');
            $getUrl=Live::getUrl();
            $form->text('push_url','推流地址')->value($getUrl[0]['push_url']);
            $form->text('play_url','播流地址')->value($getUrl[0]['live_url']);
            $form->select('goods_id','直播商品')->options(
                CmcGood::where('goods_state',1)->get()->pluck('goods_name','goods_id')
            );
            $form->display('created_at');

        });
    }
}
